import * as path from 'path'
import * as fs from 'fs'
import { call } from '../lib/awaitCall'
import Response from '../Generate/Response'

export default class CommonService {
  public static jsonData:any = null
  public static firstService(request, sender, sendResponse, next) {
    printlog("[CommonService.firstService] 111", request)
    request.data = { first: true }
    next()
  }
  public static secondService(request, sender, sendRepsonse) {
    printlog("[CommonService.secondService] 222", request)
    if (request.data?.first === true) {
      return sendRepsonse(Response(true))
    }
    return sendRepsonse(Response(false))
  }
  public static async parsePageJsonFile(request, sender, sendResponse) {
    printlog("[CommonService.parsePageJsonFile] request")
    const pageJsonFilePath = path.join(__dirname, "..", "Assets/page.json")
    const [err, data] = await call(fs.readFile, pageJsonFilePath)
    if (err) sendResponse ? sendResponse(Response(false)) : void 0
    const jsonData = JSON.parse(data)
    CommonService.jsonData = jsonData
    printlog("[CommonService.parsePageJsonFile] jsonData: ", jsonData)
    sendResponse ? sendResponse(Response(true, null, null, { data: jsonData })) : void 0
  }
}